package com.dji.sample.manage.controller;

import com.dji.sample.common.model.CustomClaim;
import com.dji.sample.manage.model.dto.CapacityDeviceDTO;
import com.dji.sample.manage.model.dto.LiveTypeDTO;
import com.dji.sample.manage.service.ILiveStreamService;
import com.dji.sdk.common.HttpResultResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.dji.sample.component.AuthInterceptor.TOKEN_CLAIM;


@RestController
@Slf4j
@RequestMapping("${url.manage.prefix}${url.manage.version}/live")
public class LiveStreamController {

    @Autowired
    private ILiveStreamService liveStreamService;

    @Autowired
    private ObjectMapper mapper;

    /**
     * 获取当前用户工作空间中所有无人机的直播能力数据
     * @param request HTTP请求对象，用于获取用户信息
     * @return 直播能力数据列表
     */
    @GetMapping("/capacity")
    public HttpResultResponse<List<CapacityDeviceDTO>> getLiveCapacity(HttpServletRequest request) {
        // 获取当前用户信息
        CustomClaim customClaim = (CustomClaim)request.getAttribute(TOKEN_CLAIM);

        List<CapacityDeviceDTO> liveCapacity = liveStreamService.getLiveCapacity(customClaim.getWorkspaceId());
        System.out.println("liveCapacity: " + liveCapacity);
        return HttpResultResponse.success(liveCapacity);
    }

    /**
     * 根据Web端传入的参数开始直播推流
     * @param liveParam 直播参数
     * @return 直播结果响应
     */
    @PostMapping("/streams/start")
    public HttpResultResponse liveStart(@RequestBody LiveTypeDTO liveParam) {
        System.out.println("------------:"+liveParam);
        return liveStreamService.liveStart(liveParam);
    }

    /**
     * 启动AI检测直播流
     * @param streamid 流ID，用于标识特定的直播流
     * @return AI检测直播启动结果，包含处理后的直播URL
     */
    @GetMapping("/streams/start/ai/{streamid}")
    public HttpResultResponse<String> liveStartAi(@PathVariable("streamid") String streamid) {
        try {
            // 参数验证
            if (streamid == null || streamid.trim().isEmpty()) {
                log.warn("AI直播启动失败：流ID不能为空");
                return HttpResultResponse.error("流ID不能为空");
            }
            
            log.info("开始启动AI检测直播，流ID: {}", streamid);
            HttpResultResponse<String> result = liveStreamService.liveStartAi(streamid.trim());
            
            if (result.getCode() == HttpResultResponse.CODE_SUCCESS) {
                log.info("AI检测直播启动成功，流ID: {}, 直播URL: {}", streamid, result.getData());
            } else {
                log.error("AI检测直播启动失败，流ID: {}, 错误信息: {}", streamid, result.getMessage());
            }
            
            return result;
        } catch (Exception e) {
            log.error("AI检测直播启动异常，流ID: {}", streamid, e);
            return HttpResultResponse.error("AI检测直播启动失败：" + e.getMessage());
        }
    }



    /**
     * 根据Web端传入的参数停止直播推流
     * @param liveParam 直播参数，包含视频ID
     * @return 直播停止结果响应
     */
    @PostMapping("/streams/stop")
    public HttpResultResponse liveStop(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveStop(liveParam.getVideoId());
    }

    /**
     * 根据Web端传入的参数设置直播质量
     * @param liveParam 直播参数
     * @return 直播质量设置结果响应
     */
    @PostMapping("/streams/update")
    public HttpResultResponse liveSetQuality(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveSetQuality(liveParam);
    }

    /**
     * 切换直播镜头
     * @param liveParam 直播参数
     * @return 镜头切换结果响应
     */
    @PostMapping("/streams/switch")
    public HttpResultResponse liveLensChange(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveLensChange(liveParam);
    }

}